package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import com.tanhua.server.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author げっしょく
 * @date 2021/10/23
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private UsersService usersService;

    /**
     * 查询用户信息
     * @param userID 需要查询的用户id
     * @return 用户的用户信息
     */
    @GetMapping
    public UserInfoVo getUserInfo(Long userID) {
        if (userID == null) {
            userID = UserHolder.getId();
        }
        return userInfoService.getUserByID(userID);
    }

    /**
     * 修改用户信息
     * @param userInfo 需要修改成的信息
     * @return
     */
    @PutMapping
    public ResponseEntity updateById(@RequestBody UserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改头像
     * @param headPhoto 需要修改的头像路径和输入流
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) throws IOException {
        String path = headPhoto.getOriginalFilename();
        InputStream inputStream = headPhoto.getInputStream();
        userInfoService.updateHeader(path,inputStream);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/counts")
    public ResponseEntity counts() {
        CountsVo countsVo = userService.counts();
        return ResponseEntity.ok(countsVo);
    }

    /**
     *
     * @param type
     *           1: 互相关注
     *           2: 我关注
     *           3: 粉丝
     *           4: 谁看过我
     * @param page
     * @param pagesize
     * @param nickname
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable int type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  String nickname) {
        PageResult pageResult = null;
        switch (type) {
            case 1:
                pageResult = usersService.getLikeMutually(page, pagesize, nickname);
                break;
            case 2:
                pageResult = usersService.getMeLikeWho(page, pagesize, nickname);
                break;
            case 3:
                pageResult = usersService.getFans(page, pagesize, nickname);
                break;
            case 4:
                pageResult = usersService.getLookMe(page, pagesize, nickname);
                break;
            default:
                break;
        }
        return ResponseEntity.ok(pageResult);
    }
    /**
     * 粉丝-喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable Long uid) {
        usersService.fansLike(uid);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleteLikeUser(@PathVariable Long uid){
        usersService.deleteLikeUser(uid);

        return ResponseEntity.ok(null);
    }
}
